package com.chen.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.model.Follow;
import com.chen.blog.model.User;
import com.chen.blog.service.FollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Api(tags = "关注管理 FollowController")
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    @ApiOperation("获取热门作者")
    @GetMapping("/hot")
    public List<Map<String, Object>> getHotAuthor() {
        return followService.getHotAuthor();
    }

    @ApiOperation("新增关注")
    @PostMapping
    public CommonResult addFollow(@RequestBody Follow follow) {
        if (followService.save(follow)) {
            return CommonResult.success(MessageConstant.FOLLOW_USER_SUCCESS);
        }
        return CommonResult.error(MessageConstant.FAIL);
    }

    @ApiOperation("删除关注")
    @DeleteMapping("/{userId}/{authorId}")
    public CommonResult deleteFollow(@PathVariable("userId") Integer userId, @PathVariable("authorId") Integer authorId) {
        if (followService.remove(new QueryWrapper<Follow>().eq("author_id", authorId).eq("user_id", userId))) {
            return CommonResult.success(MessageConstant.FOLLOW_USER_FAIL);
        }
        return CommonResult.error(MessageConstant.FAIL);
    }

    @ApiOperation("检索指定id用户的关注列表")
    @GetMapping("/{currentPage}/{pageSize}")
    public CommonResult getFollowAuthor(@PathVariable(value = "currentPage") Integer currentPage, @PathVariable(value = "pageSize") Integer pageSize, User user) {
        return followService.getByPage(currentPage, pageSize, user);
    }
}
